package com.example.demo1122.service.impl;

import com.example.demo1122.domain.LoginAdmin;
import com.example.demo1122.domain.LoginStudent;
import com.example.demo1122.domain.LoginTeacher;
import com.example.demo1122.domain.ResponseResult;
import com.example.demo1122.entity.Admin;
import com.example.demo1122.entity.Student;
import com.example.demo1122.entity.Teacher;
import com.example.demo1122.service.ILoginService;
import com.example.demo1122.utils.JwtUtil;
import com.example.demo1122.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;



@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private TeacherUserDetailsService teacherUserDetailsService;

    @Autowired
    private StudentUserDetailsService studentUserDetailsService;
    @Autowired
    private AdminUserDetailsService adminUserDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    //学生登录
    @Override
    public ResponseResult loginStudent(Student student) {
        // 用户认证
        UserDetails userDetails = studentUserDetailsService.loadUserByUsername(student.getStudentID());

        // 检查用户是否存在以及密码是否匹配
        if (userDetails != null && passwordEncoder.matches(student.getPassword(), userDetails.getPassword())) {
            Authentication authenticate = new UsernamePasswordAuthenticationToken(
                    userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
            // 认证通过的逻辑
            LoginStudent loginStudent = (LoginStudent) userDetails;
            String studentID = loginStudent.getStudent().getStudentID();
            HashMap<String, Object> claimMap = new HashMap<>();
            claimMap.put("studentID", studentID);
            claimMap.put("role", 1);
            String jwt = JwtUtil.createJWT(claimMap);
            Map<String, String> map = new HashMap<>();
            map.put("token", jwt);
            redisCache.setCacheObject("studentlogin:" + studentID, userDetails);
            return new ResponseResult(200, "登录成功", map);
        } else {
            // 认证失败的逻辑
            throw new RuntimeException("用户名或密码错误");
        }
    }


    // 老师登录
    @Override
    public ResponseResult loginTeacher(Teacher teacher) {
        // 用户认证
        UserDetails userDetails = teacherUserDetailsService.loadUserByUsername(teacher.getTeacherID());

        // 检查用户是否存在以及密码是否匹配
        if (userDetails != null && passwordEncoder.matches(teacher.getPassword(), userDetails.getPassword())) {
            Authentication authenticate = new UsernamePasswordAuthenticationToken(
                    userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());

            // 认证通过的逻辑
            LoginTeacher loginTeacher = (LoginTeacher) userDetails;
            String teacherID = loginTeacher.getTeacher().getTeacherID();
            HashMap<String, Object> claimMap = new HashMap<>();
            claimMap.put("teacherID", teacherID);
            claimMap.put("role", 2);
            String jwt = JwtUtil.createJWT(claimMap);
            Map<String, String> map = new HashMap<>();
            map.put("token", jwt);
            redisCache.setCacheObject("teacherlogin:" + teacherID, userDetails);
            return new ResponseResult(200, "登录成功", map);
        } else {
            // 认证失败的逻辑
            throw new RuntimeException("用户名或密码错误");
        }
    }


    //管理员登录
    @Override
    public ResponseResult loginAdmin(Admin admin) {
        // 用户认证
        UserDetails userDetails = adminUserDetailsService.loadUserByUsername(admin.getAdminID());

        // 检查用户是否存在以及密码是否匹配
        if (userDetails != null && passwordEncoder.matches(admin.getPassword(), userDetails.getPassword())) {
            Authentication authenticate = new UsernamePasswordAuthenticationToken(
                    userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());

            // 认证通过的逻辑
            LoginAdmin loginAdmin = (LoginAdmin) userDetails;
            String adminID = loginAdmin.getAdmin().getAdminID();
            HashMap<String, Object> claimMap = new HashMap<>();
            claimMap.put("adminID", adminID);
            claimMap.put("role", 3);
            String jwt = JwtUtil.createJWT(claimMap);
            Map<String, String> map = new HashMap<>();
            map.put("token", jwt);
            redisCache.setCacheObject("adminlogin:" + adminID, userDetails);
            return new ResponseResult(200, "登录成功", map);
        } else {
            // 认证失败的逻辑
            throw new RuntimeException("用户名或密码错误");
        }
    }
}

